package me.sdevil507.biz.fruit.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitDTO;
import me.sdevil507.biz.fruit.dto.FruitParamsDTO;
import me.sdevil507.biz.fruit.dto.ResultHomeListFruitDTO;
import me.sdevil507.biz.fruit.dto.ResultListFruitDTO;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.po.FruitPO;
import me.sdevil507.biz.fruit.service.FruitImportExportService;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.status.ApiStatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @program: tips_back_end
 * @description: 成果展示类---专家画像
 * @author: kzl
 * @create: 2020-06-23 16:39
 **/
@RestController
@RequestMapping("/api/hgp/fruit")
@Slf4j
public class FruitHgpController {

    @Autowired
    private FruitService fruitService;

    @Autowired
    private FruitImportExportService fruitImportExportService;

    @Autowired
    private FruitMapper fruitMapper;

    @PostMapping("/v1/readAllExpertHomeFruitByPage")
    public ApiResultDTO readAllExpertHomeFruitByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            Page<ResultListFruitDTO> pagePos = fruitService.readAllHomeFruitByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());


            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    @PostMapping("/v1/readAllExpertHomeFruitOfSearchByPage")
    public ApiResultDTO readAllExpertHomeFruitOfSearchByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            Page<ResultListFruitDTO> pagePos = fruitService.readAllHomeFruitOfSearchByPage(fruitParamsDTO);


            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    @PostMapping("/v1/readAllByPage")
    public ApiResultDTO getAllByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            Page<ResultListFruitDTO> pagePos = fruitService.readAllFruitByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }


    @PostMapping("/v1/readAll")
    public ApiResultDTO getAll(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            List<FruitPO> list = fruitService.readAllOrgFruit(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getSortDto());
            apiResultDto = ApiResultGenerator.create("0", "获取信息列表成功!", list);
        } catch (Exception e) {
            log.error("获取信息列表失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "获取信息列表失败!");
        }
        return apiResultDto;
    }

    /**
     * 获取单个成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/v1/getFruitById")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFruitById(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            FruitPO fruitPo = fruitService.findOneById(dto.getId());

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), fruitPo);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }

    /**
     * @ClassName FruitExeController
            * @Description : a.获取专家的机构数与对应成果的列表:
     *   ----结果：（机构名称、机构logo、当前专家在本机构的成果数）
     *   ----逻辑：查询该专家的所有成果，进行机构分组，然后获取机构和成果数
     *  b.合作过的专家列表
     *   ----结果：专家头像、名称、就职单位
     *   ----逻辑：查询该专家的所有成果，进行专家分组，然后查询专家的详细信息
            *
            * @Return : me.sdevil507.supports.result.ApiResultDTO
            * @Author : kzl
            * @Date : 2020/9/3 22:39
    */
    @PostMapping("/v1/relation")
    public ApiResultDTO relation(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        //
        try {
//            HashMap<String,Object> resultMap = fruitService.countByOrgID(fruitDTO);
            HashMap<String,Object> resultMap = fruitService.getExpertRelationData(fruitParamsDTO.getSearchDto());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);
        } catch (Exception e) {
            //事务回滚
            log.error("获取关联专家和机构信息失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取关联专家和机构信息失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取专家主页的百分比数据
     */
    @PostMapping("/getHomePercentData")
    public ApiResultDTO getHomePercentData(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        //
        try {
            HashMap<String,Object> resultMap = fruitService.getExpertHomePercentData(fruitParamsDTO.getSearchDto());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);
        } catch (Exception e) {
            //事务回滚
            log.error("获取成果百分比数据失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果百分比数据失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取专家主页的折线数据（成果和批示）
     * @return
     */
    @PostMapping("/getHomeLineData")
    public ApiResultDTO getHomeLineData(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        //
        try {
            HashMap<String,Object> resultMap = fruitService.getExpertHomeLineData(fruitParamsDTO.getSearchDto());
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultMap);
        } catch (Exception e) {
            //事务回滚
            log.error("获取成果百分比数据失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果百分比数据失败");
        }
        return apiResultDTO;
    }


}
